/*
 * @Author: TC
 * @Date: 2025-06-20 16:38:54
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-28 16:00:27
 */

import { IVec2Like, v3, Vec3 } from "cc";
import { BulletBase } from "./BulletBase";
import { ViewLogic } from "../../../Module/Game/Logic/ViewLogic";

/**
 * 子弹移动
 */
export class BulletMove {
    //当前速度大小s
    private _speed: number = 0;
    //当前方向
    private _curDir: IVec2Like = { x: 0, y: 0 };
    //线性速度
    private _velocity: Vec3 = v3();
    //延迟运动时间
    private _delayTime: number = 0;
    //总运动时间
    private _moveTime: number = 0;
    //移动物体是否需要旋转角度
    private _isNeedRotate: boolean = true;
    public get isNeedRotate(): boolean {
        return this._isNeedRotate;
    }
    /**一段移动距离 */
    private _distanceOne: number = 0;
    /**总移动距离 */
    private _distanceTotal: number = 0;
    public bullet: BulletBase = null;

    constructor(bullet: BulletBase) {
        this.bullet = bullet;
    }

    /**
     * 设置线性速度
     * @param x
     * @param y
     */
    public setVelocity(x: number, y: number, speed?: number): void {
        this._speed = speed;
        this._velocity.x = x;
        this._velocity.y = y;
        this._velocity.normalize();
        this._velocity.multiplyScalar(this._speed);
        this.setAngel(ViewLogic.getAngleByVec(this._velocity.x, this._velocity.y) % 360);
    }

    public tick(dt: number): void {
        if (this._delayTime > 0) {
            if (this._moveTime < this._delayTime) {
                this._moveTime += dt;
                return;
            }
        }

        if (this._velocity.x != 0 || this._velocity.y != 0) {
            this.bullet.node.x += dt * this._velocity.x;
            this.bullet.node.y += dt * this._velocity.y;
            this._distanceOne += dt * this._speed;
            this._distanceTotal += dt * this._speed;
        }
    }

    /**
     * 旋转更新 同步所有碰撞体更新
     * @param angle
     */
    public setAngel(angle: number) {
        if (!this.bullet.autoAngle) return;
        this.bullet.node.angle = angle;
    }

    /**
     * 重置
     */
    public reset() {
        this._moveTime = 0;
        this._delayTime = 0;
        this.setAngel(0);
        this._speed = 0;
        this._velocity.x = this._velocity.y = 0;
        // this._distanceTotal = 0;
        // this._distanceOne = 0;
        // this._distanceTargetPos = 0;
    }
}
